package cn.licoy.wdog.common.util;

import cn.licoy.wdog.core.entity.hospital.DetectionInfo;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ExportDetectionInfo {

    public static HSSFWorkbook getHSSFWorkbook(List<DetectionInfo> detectionInfoList) {
        //创建工作簿
        HSSFWorkbook workBook = new HSSFWorkbook();
        //创建工作表  工作表的名字叫helloWorld
        HSSFSheet sheet = workBook.createSheet("尿液分析报告单");

        //设置列宽度
        sheet.setColumnWidth(0, 9 * 256);
        sheet.setColumnWidth(1, 19 * 256);
        sheet.setColumnWidth(2, 2 * 256);
        sheet.setColumnWidth(3, 11 * 256);
        sheet.setColumnWidth(4, 19 * 256);
        sheet.setColumnWidth(5, 2 * 256);

        //设置打印样式
        HSSFPrintSetup ps = sheet.getPrintSetup();
        ps.setLandscape(false); // 打印方向，true：横向，false：纵向
        ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张
//        sheet.setMargin(HSSFSheet.BottomMargin,( double ) 0.5 );// 页边距（下）
//        sheet.setMargin(HSSFSheet.LeftMargin,( double ) 0.1 );// 页边距（左）
//        sheet.setMargin(HSSFSheet.RightMargin,( double ) 0.1 );// 页边距（右）
//        sheet.setMargin(HSSFSheet.TopMargin,( double ) 0.5 );// 页边距（上）
        sheet.setHorizontallyCenter(true);//设置打印页面为水平居中
        sheet.setVerticallyCenter(true);//设置打印页面为垂直居中使用POI输出Excel时打印页面的设置 - TOUGHGUYNEU - @EXPLORER使用POI输出Excel时打印页面的设置 - TOUGHGUYNEU - @EXPLORER


        /* 单元格样式 */
        //水平居中
        HSSFCellStyle centerStyle = workBook.createCellStyle();
        centerStyle.setAlignment(HorizontalAlignment.CENTER);
        //上下边框
        HSSFCellStyle borderOfTopAndBottom = workBook.createCellStyle();
        borderOfTopAndBottom.setBorderTop(BorderStyle.THIN);
        borderOfTopAndBottom.setBorderBottom(BorderStyle.THIN);
        //下边框
        HSSFCellStyle borderOfTop = workBook.createCellStyle();
        borderOfTop.setBorderTop(BorderStyle.THIN);

        /* 记录行和列 */
        int thisRow = 0;
//        int thisCell = 0;

        //记录拼装了多少条数据
        int log = 1;


        ////////////////////////开始拼装数据
        for (DetectionInfo detectionInfo : detectionInfoList) {

            /* 标题 */

            //创建行,第1行
            sheet.addMergedRegion(new CellRangeAddress(thisRow, thisRow, 0, 7));
            HSSFRow row = sheet.createRow(thisRow);
            //创建单元格，操作第1行第1列
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(centerStyle);
            cell.setCellValue("尿液分析报告单");

            /* 样本号那一行 */
            thisRow += 2;
            row = sheet.createRow(thisRow);
            //样本号
            cell = row.createCell(0);
            cell.setCellValue("样本号：");
            cell = row.createCell(1);
            cell.setCellValue(detectionInfo.getSerialNum());
            //姓名
            cell = row.createCell(3);
            cell.setCellValue("姓名：" + detectionInfo.getUsername());
            //性别
            sheet.addMergedRegion(new CellRangeAddress(thisRow, thisRow, 5, 6));
            cell = row.createCell(5);
            cell.setCellValue("性别：" + detectionInfo.getGender());
            //年龄
            cell = row.createCell(7);
            cell.setCellValue("年龄：");

            /* 表头 */
            thisRow += 1;
            row = sheet.createRow(thisRow);
            //项目
            cell = row.createCell(0);
            cell.setCellStyle(borderOfTopAndBottom);
            cell.setCellValue("项目");
            //全称
            cell = row.createCell(1);
            cell.setCellStyle(borderOfTopAndBottom);
            cell.setCellValue("全称");
            //结果
            cell = row.createCell(4);
            cell.setCellStyle(borderOfTopAndBottom);
            cell.setCellValue("结果");
            //参考值
            cell = row.createCell(7);
            cell.setCellStyle(borderOfTopAndBottom);
            cell.setCellValue("参考值");
            //补充边框
            cell = row.createCell(2);
            cell.setCellStyle(borderOfTopAndBottom);
            cell = row.createCell(3);
            cell.setCellStyle(borderOfTopAndBottom);
            cell = row.createCell(5);
            cell.setCellStyle(borderOfTopAndBottom);
            cell = row.createCell(6);
            cell.setCellStyle(borderOfTopAndBottom);

            /* LEU */
            thisRow += 1;
            row = sheet.createRow(thisRow);
            cell = row.createCell(0);
            cell.setCellValue("LEU");
            cell = row.createCell(1);
            cell.setCellValue("白细胞");
            cell = row.createCell(4);
            cell.setCellValue(detectionInfo.getLeu());
            if (!"-".equals(detectionInfo.getLeu()) && detectionInfo.getLeu() != null) {
                cell = row.createCell(5);
                cell.setCellValue("↑");
            }
            cell = row.createCell(7);
            cell.setCellValue("阴性");
            /* NIT */
            thisRow += 1;
            row = sheet.createRow(thisRow);
            cell = row.createCell(0);
            cell.setCellValue("NIT");
            cell = row.createCell(1);
            cell.setCellValue("亚硝酸盐");
            cell = row.createCell(4);
            cell.setCellValue(detectionInfo.getNit());
            if (!"-".equals(detectionInfo.getNit()) && detectionInfo.getNit() != null) {
                cell = row.createCell(5);
                cell.setCellValue("↑");
            }
            cell = row.createCell(7);
            cell.setCellValue("阴性");
            /* UBG */
            thisRow += 1;
            row = sheet.createRow(thisRow);
            cell = row.createCell(0);
            cell.setCellValue("UBG");
            cell = row.createCell(1);
            cell.setCellValue("尿胆原");
            cell = row.createCell(4);
            cell.setCellValue(detectionInfo.getUbg());
            if (!"-".equals(detectionInfo.getUbg()) && detectionInfo.getUbg() != null) {
                cell = row.createCell(5);
                cell.setCellValue("↑");
            }
            cell = row.createCell(7);
            cell.setCellValue("阴性");
            /* PRO */
            thisRow += 1;
            row = sheet.createRow(thisRow);
            cell = row.createCell(0);
            cell.setCellValue("PRO");
            cell = row.createCell(1);
            cell.setCellValue("蛋白质");
            cell = row.createCell(4);
            cell.setCellValue(detectionInfo.getPro());
            if (!"-".equals(detectionInfo.getPro()) && detectionInfo.getPro() != null) {
                cell = row.createCell(5);
                cell.setCellValue("↑");
            }
            cell = row.createCell(7);
            cell.setCellValue("阴性");
            /* PH */
            thisRow += 1;
            row = sheet.createRow(thisRow);
            cell = row.createCell(0);
            cell.setCellValue("pH");
            cell = row.createCell(1);
            cell.setCellValue("pH值");
            cell = row.createCell(4);
            cell.setCellValue(detectionInfo.getPh());
            if (Double.parseDouble(detectionInfo.getPh()) < 4.5) {
                cell = row.createCell(5);
                cell.setCellValue("↓");
            } else if (Double.parseDouble(detectionInfo.getPh()) > 8.0) {
                cell = row.createCell(5);
                cell.setCellValue("↑");
            }
            cell = row.createCell(7);
            cell.setCellValue("7");
            /* BLD */
            thisRow += 1;
            row = sheet.createRow(thisRow);
            cell = row.createCell(0);
            cell.setCellValue("BLD");
            cell = row.createCell(1);
            cell.setCellValue("潜血");
            cell = row.createCell(4);
            cell.setCellValue(detectionInfo.getBld());
            if (!"-".equals(detectionInfo.getBld()) && detectionInfo.getBld() != null) {
                cell = row.createCell(5);
                cell.setCellValue("↑");
            }
            cell = row.createCell(7);
            cell.setCellValue("阴性");
            /* SG */
            thisRow += 1;
            row = sheet.createRow(thisRow);
            cell = row.createCell(0);
            cell.setCellValue("SG");
            cell = row.createCell(1);
            cell.setCellValue("比重");
            cell = row.createCell(4);
            cell.setCellValue(detectionInfo.getSg());
            if (Double.parseDouble(detectionInfo.getSg()) < 1.015) {
                cell = row.createCell(5);
                cell.setCellValue("↓");
            } else if (Double.parseDouble(detectionInfo.getSg()) > 1.025) {
                cell = row.createCell(5);
                cell.setCellValue("↑");
            }
            cell = row.createCell(7);
            cell.setCellValue("1.0—2.0");
            /* KET */
            thisRow += 1;
            row = sheet.createRow(thisRow);
            cell = row.createCell(0);
            cell.setCellValue("KET");
            cell = row.createCell(1);
            cell.setCellValue("酮体");
            cell = row.createCell(4);
            cell.setCellValue(detectionInfo.getKet());
            if (!"-".equals(detectionInfo.getKet()) && detectionInfo.getKet() != null) {
                cell = row.createCell(5);
                cell.setCellValue("↑");
            }
            cell = row.createCell(7);
            cell.setCellValue("阴性");
            /* BIL */
            thisRow += 1;
            row = sheet.createRow(thisRow);
            cell = row.createCell(0);
            cell.setCellValue("BIL");
            cell = row.createCell(1);
            cell.setCellValue("胆红素");
            cell = row.createCell(4);
            cell.setCellValue(detectionInfo.getBil());
            if (!"-".equals(detectionInfo.getBil()) && detectionInfo.getBil() != null) {
                cell = row.createCell(5);
                cell.setCellValue("↑");
            }
            cell = row.createCell(7);
            cell.setCellValue("阴性");
            /* GLU */
            thisRow += 1;
            row = sheet.createRow(thisRow);
            cell = row.createCell(0);
            cell.setCellValue("GLU");
            cell = row.createCell(1);
            cell.setCellValue("葡萄糖");
            cell = row.createCell(4);
            cell.setCellValue(detectionInfo.getGlu());
            if (!"-".equals(detectionInfo.getGlu()) && detectionInfo.getGlu() != null) {
                cell = row.createCell(5);
                cell.setCellValue("↑");
            }
            cell = row.createCell(7);
            cell.setCellValue("阴性");
            /* VC */
            thisRow += 1;
            row = sheet.createRow(thisRow);
            cell = row.createCell(0);
            cell.setCellValue("VC");
            cell = row.createCell(1);
            cell.setCellValue("维生素C");
            cell = row.createCell(4);
            cell.setCellValue(detectionInfo.getVc());
            if (!"-".equals(detectionInfo.getVc()) && detectionInfo.getVc() != null) {
                cell = row.createCell(5);
                cell.setCellValue("↑");
            }
            cell = row.createCell(7);
            cell.setCellValue("阴性");

            /* 送检医生一行 */
            thisRow += 1;
            row = sheet.createRow(thisRow);
            cell = row.createCell(0);
            cell.setCellStyle(borderOfTop);
            cell.setCellValue("送检医生：");
            cell = row.createCell(3);
            cell.setCellStyle(borderOfTop);
            cell.setCellValue("检验医生：");
            cell = row.createCell(6);
            cell.setCellStyle(borderOfTop);
            cell.setCellValue("审核：");
            //填充边框
            cell = row.createCell(1);
            cell.setCellStyle(borderOfTop);
            cell = row.createCell(2);
            cell.setCellStyle(borderOfTop);
            cell = row.createCell(4);
            cell.setCellStyle(borderOfTop);
            cell = row.createCell(5);
            cell.setCellStyle(borderOfTop);
            cell = row.createCell(7);
            cell.setCellStyle(borderOfTop);

            /* 测试日期一行 */
            thisRow += 1;
            row = sheet.createRow(thisRow);
            cell = row.createCell(0);
            cell.setCellValue("测试日期：");
            cell = row.createCell(1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            cell.setCellValue(sdf.format(detectionInfo.getDetectionDate()));
            cell = row.createCell(3);
            cell.setCellValue("打印日期：");
            cell = row.createCell(4);
            cell.setCellValue(sdf.format(new Date()));
            cell = row.createCell(6);
            cell.setCellValue("地址：");
            cell = row.createCell(7);
            cell.setCellValue(detectionInfo.getRegion());
            //备注
            thisRow += 1;
            row = sheet.createRow(thisRow);
            sheet.addMergedRegion(new CellRangeAddress(thisRow, thisRow, 4, 6));
            cell = row.createCell(4);
            cell.setCellValue("(此结果只针对本次样本测试)");

            if (log % 3 == 0){
                thisRow += 1;
            }else {
                thisRow += 2;
            }
            log ++;

        }
        return workBook;
    }
}
